package com.sendpo.zjx.sendpo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.fragment.songlist.BaseSongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.DaodiansongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.DianqingsongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.FenxiangsongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.FujingsongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.HuodongsongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.KaiyesongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.QingcangsongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.TiyansongFragment;
import com.sendpo.zjx.sendpo.fragment.songlist.WenjuansongFragment;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;
import com.sendpo.zjx.sendpo.views.MainViewpager;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/18.
 */

public class SongListActivity extends AppCompatActivity {

    @BindView(R.id.backbt)
    RelativeLayout backbt;
    @BindView(R.id.right_head)
    ImageView rightHead;
    @BindView(R.id.head)
    RelativeLayout head;
    @BindView(R.id.fujinsong_tv)
    TextView fujinsongTv;
    @BindView(R.id.fujinsong_rl)
    RelativeLayout fujinsongRl;
    @BindView(R.id.daodiansong_tv)
    TextView daodiansongTv;
    @BindView(R.id.daodiansong_rl)
    RelativeLayout daodiansongRl;
    @BindView(R.id.qingcangsong_tv)
    TextView qingcangsongTv;
    @BindView(R.id.qingcangsong_rl)
    RelativeLayout qingcangsongRl;
    @BindView(R.id.huodongsong_tv)
    TextView huodongsongTv;
    @BindView(R.id.huodongsong_rl)
    RelativeLayout huodongsongRl;
    @BindView(R.id.kaiyesong_rl)
    RelativeLayout kaiyesongRl;
    @BindView(R.id.wenjuansong_tv)
    TextView wenjuansongTv;
    @BindView(R.id.wenjuansong_rl)
    RelativeLayout wenjuansongRl;
    @BindView(R.id.dianqingsong_tv)
    TextView dianqingsongTv;
    @BindView(R.id.dianqingsong_rl)
    RelativeLayout dianqingsongRl;
    @BindView(R.id.tiyansong_tv)
    TextView tiyansongTv;
    @BindView(R.id.tiyansong_rl)
    RelativeLayout tiyansongRl;
    @BindView(R.id.fenxiangsong_tv)
    TextView fenxiangsongTv;
    @BindView(R.id.fenxiangsong_rl)
    RelativeLayout fenxiangsongRl;
    @BindView(R.id.mainviewpager)
    MainViewpager mainviewpager;
    @BindView(R.id.kaiyesong_tv)
    TextView kaiyesongTv;
    private FujingsongFragment fujingsong;
    private DaodiansongFragment daodiansong;
    private QingcangsongFragment qingcangsong;
    private HuodongsongFragment huodongsong;
    private KaiyesongFragment kaiyesong;
    private WenjuansongFragment wenjuansong;
    private DianqingsongFragment dianqingsong;
    private TiyansongFragment tiyansong;
    private FenxiangsongFragment fenxiangsong;
       @BindView(value = R.id.search_edit)
        EditText search_edit;

       public int position=0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_songlist);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }

    private void initComponent() {
        List<BaseSongFragment> mainFragments = new ArrayList<>();
        mainFragments.add(fujingsong = new FujingsongFragment());
        mainFragments.add(daodiansong = new DaodiansongFragment());
        mainFragments.add(qingcangsong = new QingcangsongFragment());
        mainFragments.add(huodongsong = new HuodongsongFragment());
        mainFragments.add(kaiyesong = new KaiyesongFragment());
        mainFragments.add(wenjuansong = new WenjuansongFragment());
        mainFragments.add(dianqingsong = new DianqingsongFragment());
        mainFragments.add(tiyansong = new TiyansongFragment());
        mainFragments.add(fenxiangsong = new FenxiangsongFragment());
        mainviewpager.setOffscreenPageLimit(1);
        mainviewpager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager(), mainFragments));
        position=getIntent().getIntExtra("position",0);
        setItem(position);
    }

    @OnClick(value = {R.id.backbt, R.id.fujinsong_rl
            , R.id.daodiansong_rl,
    R.id.qingcangsong_rl, R.id.huodongsong_rl,
    R.id.kaiyesong_rl,
    R.id.wenjuansong_rl,
    R.id.dianqingsong_rl, R.id.tiyansong_rl,
    R.id.fenxiangsong_rl,
    R.id.right_head})
    public void onMyClick(View view) {
        switch (view.getId()) {
            case R.id.backbt:
                finish();
                break;
            case R.id.fujinsong_rl:
                setItem(0);
                break;
            case R.id.daodiansong_rl:
                setItem(1);
                break;
            case R.id.qingcangsong_rl:
                setItem(2);
                break;
            case R.id.huodongsong_rl:
                setItem(3);
                break;
            case R.id.kaiyesong_rl:
                setItem(4);
                break;
            case R.id.wenjuansong_rl:
                setItem(5);
                break;
            case R.id.dianqingsong_rl:
                setItem(6);
                break;
            case R.id.tiyansong_rl:
                setItem(7);
                break;
            case R.id.fenxiangsong_rl:
                setItem(8);
                break;
            case R.id.right_head:
                  if(TextUtils.isEmpty(search_edit.getText().toString().trim())){
                      Toast.makeText(getApplicationContext(),"内容不能为空",Toast.LENGTH_SHORT).show();
                      return;
                  }
                Map<String,Object> map=new HashMap<>();
                map.put("data",search_edit.getText().toString().trim());
                Tools.redirectActivity(this,StoreSearchActivity.class,map);
                webaddHistory(search_edit.getText().toString().trim());
                break;
        }
    }

    public void webaddHistory(String content){
        Map<String,String> map=new HashMap<>();
        map.put("searchContent",""+content);
        Xutils.POST(HttpUrl.searchmerchandiseaddhistory, map, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    switch (jsonObject.getInt("state")){
                        case HttpCode.SUCCESS:
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    public class MainViewPagerAdapter extends FragmentPagerAdapter {
        public List<BaseSongFragment> list;

        public MainViewPagerAdapter(FragmentManager fm, List<BaseSongFragment> list1) {
            super(fm);
            this.list = list1;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
    public void setItem(int position) {
        switch (position) {
            case 0:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_theme));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 1:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_theme));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 2:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_theme));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 3:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_theme));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 4:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_theme));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 5:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_theme));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 6:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_theme));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 7:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_theme));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_gray));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
            case 8:
                fujinsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                fujinsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                daodiansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                daodiansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                qingcangsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                qingcangsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                huodongsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                huodongsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                kaiyesongRl.setBackgroundColor(getResources().getColor(R.color.white));
                kaiyesongTv.setTextColor(getResources().getColor(R.color.default_gray));

                wenjuansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                wenjuansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                dianqingsongRl.setBackgroundColor(getResources().getColor(R.color.white));
                dianqingsongTv.setTextColor(getResources().getColor(R.color.default_gray));

                tiyansongRl.setBackgroundColor(getResources().getColor(R.color.white));
                tiyansongTv.setTextColor(getResources().getColor(R.color.default_gray));

                fenxiangsongRl.setBackgroundColor(getResources().getColor(R.color.default_lightgray));
                fenxiangsongTv.setTextColor(getResources().getColor(R.color.default_theme));
                mainviewpager.isScroll = true;
                mainviewpager.setCurrentItem(position, false);
                mainviewpager.isScroll = false;
                break;
        }
    }
}
